package com.execjar;

import cn.hutool.extra.ftp.Ftp;
import cn.hutool.extra.ftp.FtpMode;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;

/**
 * @author zhuzi
 * 2024/11/25 23:34
 */
@Slf4j
public class FtpApp {

    public static void mains(String[] args) {
        System.out.println("上FTP文件");
        //图片储存本地临时目录
        String ftp_host = "10.14.17.214";


        try (Ftp ftp = new Ftp(ftp_host, 21, "photo", "123456")) {
            ftp.setMode(FtpMode.Passive);
            //上传本地文件|加了斜杆是绝对路径，但是开发环境是 /data/ftp开头
            System.out.println(ftp.pwd());
            ftp.mkDirs("/grq/01");
            System.out.println(ftp.mkdir("grq/02"));
            ftp.mkDirs("grq/05");
            boolean upload = ftp.upload("/grq/", "1.jpg", new File("/root/123.png"));
            if (!upload) {//上传成功:删除图片
                log.info("文件上传FTP失败");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main2(String[] args) {
        System.out.println("上FTP文件");
        //图片储存本地临时目录
        String ftp_host = "192.169.3.170";


        try (Ftp ftp = new Ftp(ftp_host, 21, "ftpuser", "User123456")) {
            System.out.println(ftp.pwd());
            ftp.mkDirs("goods/passPhoto/grq");

            boolean upload = ftp.upload("goods/passPhoto/20241126", "grq1.jpg", new File("/tmp/grq.jpg"));
            boolean upload2 = ftp.upload("/goods/passPhoto/20241126/", "grq2.jpg", new File("/tmp/grq.jpg"));
            if (!upload) {//上传成功:删除图片
                log.info("文件上传FTP失败");
            }
            System.out.println("上传+" + upload2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
